package com.ruoyi.web.xxj.service.impl;

import java.util.List;

import com.ruoyi.web.xxj.service.IWxPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.xxj.domain.ProductOrder;
import com.ruoyi.web.xxj.domain.pojo.OrderReq;
import com.ruoyi.web.xxj.domain.vo.ClassOrderVo;
import com.ruoyi.web.xxj.domain.vo.OrderVo;
import com.ruoyi.web.xxj.enums.PayStatus;
import com.ruoyi.web.xxj.mapper.ClassOrderMapper;
import com.ruoyi.web.xxj.mapper.OrderMapper;
import com.ruoyi.web.xxj.mapper.ProductOrderMapper;
import com.ruoyi.web.xxj.service.IOrderService;

@Service
public class OrderServiceImpl implements IOrderService {
    private static final Logger log = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    private ClassOrderMapper classOrderMapper;
    @Autowired
    private ProductOrderMapper productOrderMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private IWxPayService wxPayService;

    @Override
    public int auditCancle(String type, Long id, String auditStatus, String rejectReason) {

        if (!SecurityUtils.isAdmin(SecurityUtils.getLoginUser().getUserId())) {
            throw new ServiceException("非管理员没有审核权限！");
        }
        if (StringUtils.isEmpty(type) || (!type.equals("class") && !type.equals("product"))) {
            throw new ServiceException("审核类型有误，必须是class或者product！");
        }

        ClassOrderVo classOrder = null;
        ProductOrder productOrder = null;

        if (type.equals("class")) {
        	classOrder = classOrderMapper.selectClassOrderById(id);
            if (!classOrder.getPayStatus().equals(PayStatus.REFUND_TO_AUDIT.getValue())) {
                log.info("订单ID[" + classOrder.getId() + "],课程名称[" + classOrder.getClassName() +
                        "]的状态为[" + PayStatus.getNameByValue(classOrder.getPayStatus()) + "]");
                throw new ServiceException("该课程订单状态状态异常，无法审核退款！");
            }
            //下方为退款流程，退款流程成功后，修改订单状态，支付状态：0-未支付；1-已支付；2-退款待审核；3-退款通过审核；4-拒绝退款；5-已退款；6-退款失败；7-取消订单
            if (auditStatus.equals(PayStatus.REFUND_REJECT.getValue())) {
                log.info("拒绝退款：订单ID[" + classOrder.getId() + "],课程名称[" + classOrder.getClassName() + "，拒绝退款理由【" + rejectReason + "】");
                classOrder.setPayStatus(PayStatus.REFUND_REJECT.getValue());
                classOrder.setUpdateBy(SecurityUtils.getUsername());
                classOrder.setUpdateTime(DateUtils.getNowDate());
                classOrder.setRejectCancelReason(rejectReason);
                classOrder.setRemark("拒绝退款！");
                return classOrderMapper.updateClassOrder(classOrder);
            }
             return wxPayService.refund(classOrder);
        } else {
            productOrder = productOrderMapper.selectProductOrderById(id);
            if (!productOrder.getPayStatus().equals(PayStatus.REFUND_TO_AUDIT.getValue())) {
                log.info("订单ID[" + productOrder.getId() + "],文创名称[" + productOrder.getProductName() +
                        "]的状态为[" + PayStatus.getNameByValue(productOrder.getPayStatus()) + "]");
                throw new ServiceException("该文创订单状态状态异常，无法审核退款！");
            }
            //下方为退款流程，退款流程成功后，修改订单状态，支付状态：0-未支付；1-已支付；2-退款待审核；3-退款通过审核；4-拒绝退款；5-已退款；6-退款失败
            if (auditStatus.equals(PayStatus.REFUND_REJECT.getValue())) {
                log.info("拒绝退款：订单ID[" + productOrder.getId() + "],文创名称[" + productOrder.getProductName() + "，拒绝退款理由【" + rejectReason + "】");
                productOrder.setPayStatus(PayStatus.REFUND_REJECT.getValue());
                productOrder.setUpdateBy(SecurityUtils.getUsername());
                productOrder.setUpdateTime(DateUtils.getNowDate());
                productOrder.setRejectCancelReason(rejectReason);
                productOrder.setRemark("拒绝退款！");
                return productOrderMapper.updateProductOrder(productOrder);
            }
            return wxPayService.refund(productOrder);
        }
    }

    @Override
    public List<OrderVo> selectOrderVos(OrderReq orderReq) {
        return orderMapper.selectOrderVos(orderReq);
    }

	@Override
	public OrderVo selectClassOrderVo(OrderReq orderReq) {
		return orderMapper.selectClassOrderVo(orderReq);
	}

	@Override
	public OrderVo selectProductOrderVo(OrderReq orderReq) {
		return orderMapper.selectProductOrderVo(orderReq);
	}

}
